package com.pdwbdn.views;

import com.pdwbdn.db.AccountsDao;
import com.pdwbdn.db.RegResult;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.text.AbstractDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ForgetPwdDialog extends JDialog implements ActionListener {
    private final JTextField usernameField;
    private final JTextField qqField;
    private final JTextField passwordField;
    private long clickTimestamp;//记下点击时间防止双击
    public ForgetPwdDialog(JFrame frame) {
        super(frame, "重置密码", true);
        this.setSize(300, 200);
        this.setResizable(false);//不允许调整大小
        this.setLocationRelativeTo(frame);//显示位置
        this.setUndecorated(true);//去掉最小化关闭那个边框
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//设置默认关闭操作
        this.setLayout(new BorderLayout());

        JPanel centerPanel = new JPanel(new GridLayout(3, 1));
        JPanel usernamePanel = new JPanel(new GridLayout(1, 1));
        usernamePanel.setBorder(new TitledBorder(null, "帐号", 0, 0, MyFont.font14));
        usernameField = new JTextField();
        usernameField.setFont(MyFont.font14);
        AbstractDocument usernameDocument = (AbstractDocument) usernameField.getDocument();
        usernameDocument.setDocumentFilter(new Abc123DocumentFilter());
        usernamePanel.add(usernameField);
        centerPanel.add(usernamePanel);

        JPanel qqPanel = new JPanel(new GridLayout(1, 1));
        qqPanel.setBorder(new TitledBorder(null, "注册时填写的QQ号", 0, 0, MyFont.font14));
        qqField = new JTextField();
        qqField.setFont(MyFont.font14);
        AbstractDocument qqDocument = (AbstractDocument) qqField.getDocument();
        qqDocument.setDocumentFilter(new NumberDocumentFilter());//过滤输入只能填写数字
        qqPanel.add(qqField);
        centerPanel.add(qqPanel);

        JPanel passwordPanel = new JPanel(new GridLayout(1, 1));
        passwordPanel.setBorder(new TitledBorder(null, "输入新密码", 0, 0, MyFont.font14));
        passwordField = new JTextField();
        passwordField.setFont(MyFont.font14);
        AbstractDocument passwordDocument = (AbstractDocument) passwordField.getDocument();
        passwordDocument.setDocumentFilter(new Abc123DocumentFilter());
        passwordPanel.add(passwordField);
        centerPanel.add(passwordPanel);

        this.getContentPane().add(centerPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new GridLayout(1, 2));
        JButton regButton = new JButton("重置密码");
        regButton.setFont(MyFont.font14);
        regButton.setActionCommand("forget");
        regButton.addActionListener(this);
        JButton cancelButton = new JButton("取消");
        cancelButton.setFont(MyFont.font14);
        cancelButton.setActionCommand("cancel");
        cancelButton.addActionListener(this);
        buttonPanel.add(regButton);
        buttonPanel.add(cancelButton);
        this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("cancel".equals(e.getActionCommand())) {
            this.dispose();
        } else if ("forget".equals(e.getActionCommand())) {
            try {
                long now = System.currentTimeMillis();
                if (Math.abs(now - clickTimestamp) < 3000) {
                    return;
                } else {
                    clickTimestamp = now;
                }
                String username = usernameField.getText();
                if (username == null || username.isEmpty()) {
                    MsgDialog.getInstance().showMsg("请填写帐号");
                    return;
                }
                if (username.length() > 20) {
                    MsgDialog.getInstance().showMsg("帐号长度小于20");
                    return;
                }
                String qq = qqField.getText();
                if (qq == null || qq.isEmpty()) {
                    MsgDialog.getInstance().showMsg("请填写QQ号");
                    return;
                }
                if (qq.length() != 9 && qq.length() != 10) {
                    MsgDialog.getInstance().showMsg("请填写正确QQ号");
                    return;
                }
                String password = passwordField.getText();
                if (password == null || password.isEmpty()) {
                    MsgDialog.getInstance().showMsg("请填写密码");
                    return;
                }
                if (password.length() < 6 || password.length() > 20) {
                    MsgDialog.getInstance().showMsg("密码长度6-20");
                    return;
                }
                RegResult regResult = AccountsDao.resetPassword(username, qq, password);
                if (regResult.isSuccess()) {
                    this.dispose();
                    MsgDialog.getInstance().showMsg("重置密码成功");
                } else {
                    MsgDialog.getInstance().showMsg(regResult.getMsg());
                }
            } catch (Throwable t) {
                MsgDialog.getInstance().showMsg(t.toString());
            }
        }
    }
}
